package com.zy.scwuser.send;

import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.zy.scwcommon.bean.AppResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SendSms {
    //import org.springframework.beans.factory.annotation.Value;不要导错包
    @Value("${sms.accessKeyId}")
    String accessKeyId;
    @Value("${sms.accessKeySecret}")
    String accessKeySecret;
    @Value("${sms.phoneNum}")
    String phoneNum;  //手机号
    @Value("${sms.signName}")
    String signName; //签名
    @Value("${sms.verifyCode}")
    String verifyCode; //验证码
    @Value("${sms.templateCode}")
    String templateCode; //短信模版

    public AppResponse<String> testSendSms() {
        System.out.println("----------测试短信发送--------");
        try {
            SendSmsResponse response = SmsUtil.sendVerifyCode(phoneNum,signName, verifyCode, templateCode,accessKeyId,accessKeySecret);
            System.out.println("短信接口返回的数据----------------");
            System.out.println("Code=" + response.getCode());
            System.out.println("Message=" + response.getMessage());
            System.out.println("RequestId=" + response.getRequestId());
            System.out.println("BizId=" + response.getBizId());//消息回执id
            System.out.println("======respones======" + response.toString());//

            Thread.sleep(3000L);
            // 查明细
            if (response.getCode() != null && response.getCode().equals("OK")) {
                System.out.println("-------------------查看短息发送状态--------------");

                QuerySendDetailsResponse querySendDetailsResponse = SmsUtil.querySendDetails(phoneNum, response.getBizId(), accessKeyId, accessKeySecret);
                System.out.println("短信明细查询接口返回数据----------------");
                System.out.println("Code=" + querySendDetailsResponse.getCode());
                System.out.println("Message=" + querySendDetailsResponse.getMessage());
                int i = 0;
                for (QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse
                        .getSmsSendDetailDTOs()) {
                    System.out.println("SmsSendDetailDTO[" + i + "]:");
                    System.out.println("Content=" + smsSendDetailDTO.getContent());
                    System.out.println("ErrCode=" + smsSendDetailDTO.getErrCode());
                    System.out.println("OutId=" + smsSendDetailDTO.getOutId());
                    System.out.println("PhoneNum=" + smsSendDetailDTO.getPhoneNum());
                    System.out.println("ReceiveDate=" + smsSendDetailDTO.getReceiveDate());
                    System.out.println("SendDate=" + smsSendDetailDTO.getSendDate());
                    System.out.println("SendStatus=" + smsSendDetailDTO.getSendStatus());
                    System.out.println("Template=" + smsSendDetailDTO.getTemplateCode());
                }
                System.out.println("TotalCount=" + querySendDetailsResponse.getTotalCount());
                System.out.println("RequestId=" + querySendDetailsResponse.getRequestId());
            }
            return AppResponse.ok(response.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return AppResponse.fail(null);
        }

    }
}
